package b.b.g.i;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.KeyCharacterMap.KeyData;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewConfiguration;
import b.i.k.s;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/* compiled from: MenuBuilder */
public class g implements b.i.f.a.a {
    public static final int[] y = new int[]{1, 4, 5, 3, 2, 0};
    public final Context a;
    public final Resources b;
    public boolean c;
    public boolean d;
    public a e;
    public ArrayList<i> f;
    public ArrayList<i> g;
    public boolean h;
    public ArrayList<i> i;
    public ArrayList<i> j;
    public boolean k;
    public int l = 1;
    public CharSequence m;
    public Drawable n;
    public View o;
    public boolean p = false;
    public boolean q = false;
    public boolean r = false;
    public boolean s = false;
    public ArrayList<i> t = new ArrayList();
    public CopyOnWriteArrayList<WeakReference<m>> u = new CopyOnWriteArrayList();
    public i v;
    public boolean w = false;
    public boolean x;

    /* compiled from: MenuBuilder */
    public interface a {
        boolean a(g gVar, MenuItem menuItem);

        void b(g gVar);
    }

    /* compiled from: MenuBuilder */
    public interface b {
        boolean a(i iVar);
    }

    public g(Context context) {
        boolean z = false;
        this.a = context;
        Resources resources = context.getResources();
        this.b = resources;
        this.f = new ArrayList();
        this.g = new ArrayList();
        this.h = true;
        this.i = new ArrayList();
        this.j = new ArrayList();
        this.k = true;
        if (resources.getConfiguration().keyboard != 1) {
            boolean shouldShowMenuShortcutsWhenKeyboardPresent;
            ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
            Method method = s.a;
            if (VERSION.SDK_INT >= 28) {
                shouldShowMenuShortcutsWhenKeyboardPresent = viewConfiguration.shouldShowMenuShortcutsWhenKeyboardPresent();
            } else {
                Resources resources2 = context.getResources();
                int identifier = resources2.getIdentifier("config_showMenuShortcutsWhenKeyboardPresent", "bool", "android");
                shouldShowMenuShortcutsWhenKeyboardPresent = identifier != 0 && resources2.getBoolean(identifier);
            }
            if (shouldShowMenuShortcutsWhenKeyboardPresent) {
                z = true;
            }
        }
        this.d = z;
    }

    public MenuItem a(int i, int i2, int i3, CharSequence charSequence) {
        int i4 = (-65536 & i3) >> 16;
        if (i4 >= 0) {
            int[] iArr = y;
            if (i4 < iArr.length) {
                i4 = (iArr[i4] << 16) | (65535 & i3);
                i iVar = new i(this, i, i2, i3, i4, charSequence, this.l);
                ArrayList arrayList = this.f;
                i2 = arrayList.size();
                do {
                    i2--;
                    if (i2 < 0) {
                        i2 = 0;
                        break;
                    }
                } while (((i) arrayList.get(i2)).d > i4);
                i2++;
                arrayList.add(i2, iVar);
                q(true);
                return iVar;
            }
        }
        throw new IllegalArgumentException("order does not contain a valid category.");
    }

    public MenuItem add(CharSequence charSequence) {
        return a(0, 0, 0, charSequence);
    }

    public int addIntentOptions(int i, int i2, int i3, ComponentName componentName, Intent[] intentArr, Intent intent, int i4, MenuItem[] menuItemArr) {
        PackageManager packageManager = this.a.getPackageManager();
        int i5 = 0;
        List queryIntentActivityOptions = packageManager.queryIntentActivityOptions(componentName, intentArr, intent, 0);
        int size = queryIntentActivityOptions != null ? queryIntentActivityOptions.size() : 0;
        if ((i4 & 1) == 0) {
            removeGroup(i);
        }
        while (i5 < size) {
            ResolveInfo resolveInfo = (ResolveInfo) queryIntentActivityOptions.get(i5);
            int i6 = resolveInfo.specificIndex;
            Intent intent2 = new Intent(i6 < 0 ? intent : intentArr[i6]);
            ActivityInfo activityInfo = resolveInfo.activityInfo;
            intent2.setComponent(new ComponentName(activityInfo.applicationInfo.packageName, activityInfo.name));
            i iVar = (i) a(i, i2, i3, resolveInfo.loadLabel(packageManager));
            iVar.setIcon(resolveInfo.loadIcon(packageManager));
            iVar.setIntent(intent2);
            if (menuItemArr != null) {
                i4 = resolveInfo.specificIndex;
                if (i4 >= 0) {
                    menuItemArr[i4] = iVar;
                }
            }
            i5++;
        }
        return size;
    }

    public SubMenu addSubMenu(CharSequence charSequence) {
        return addSubMenu(0, 0, 0, charSequence);
    }

    public void b(m mVar, Context context) {
        this.u.add(new WeakReference(mVar));
        mVar.c(context, this);
        this.k = true;
    }

    public final void c(boolean z) {
        if (!this.s) {
            this.s = true;
            Iterator it = this.u.iterator();
            while (it.hasNext()) {
                WeakReference weakReference = (WeakReference) it.next();
                m mVar = (m) weakReference.get();
                if (mVar == null) {
                    this.u.remove(weakReference);
                } else {
                    mVar.a(this, z);
                }
            }
            this.s = false;
        }
    }

    public void clear() {
        i iVar = this.v;
        if (iVar != null) {
            d(iVar);
        }
        this.f.clear();
        q(true);
    }

    public void clearHeader() {
        this.n = null;
        this.m = null;
        this.o = null;
        q(false);
    }

    public void close() {
        c(true);
    }

    public boolean d(i iVar) {
        boolean z = false;
        if (!this.u.isEmpty() && this.v == iVar) {
            z();
            Iterator it = this.u.iterator();
            while (it.hasNext()) {
                WeakReference weakReference = (WeakReference) it.next();
                m mVar = (m) weakReference.get();
                if (mVar == null) {
                    this.u.remove(weakReference);
                } else {
                    z = mVar.i(this, iVar);
                    if (z) {
                        break;
                    }
                }
            }
            y();
            if (z) {
                this.v = null;
            }
        }
        return z;
    }

    public boolean e(g gVar, MenuItem menuItem) {
        a aVar = this.e;
        return aVar != null && aVar.a(gVar, menuItem);
    }

    public boolean f(i iVar) {
        boolean z = false;
        if (this.u.isEmpty()) {
            return false;
        }
        z();
        Iterator it = this.u.iterator();
        while (it.hasNext()) {
            WeakReference weakReference = (WeakReference) it.next();
            m mVar = (m) weakReference.get();
            if (mVar == null) {
                this.u.remove(weakReference);
            } else {
                z = mVar.j(this, iVar);
                if (z) {
                    break;
                }
            }
        }
        y();
        if (z) {
            this.v = iVar;
        }
        return z;
    }

    public MenuItem findItem(int i) {
        int size = size();
        for (int i2 = 0; i2 < size; i2++) {
            i iVar = (i) this.f.get(i2);
            if (iVar.a == i) {
                return iVar;
            }
            if (iVar.hasSubMenu()) {
                MenuItem findItem = iVar.o.findItem(i);
                if (findItem != null) {
                    return findItem;
                }
            }
        }
        return null;
    }

    public i g(int i, KeyEvent keyEvent) {
        ArrayList arrayList = this.t;
        arrayList.clear();
        h(arrayList, i, keyEvent);
        if (arrayList.isEmpty()) {
            return null;
        }
        int metaState = keyEvent.getMetaState();
        KeyData keyData = new KeyData();
        keyEvent.getKeyData(keyData);
        int size = arrayList.size();
        if (size == 1) {
            return (i) arrayList.get(0);
        }
        boolean n = n();
        for (int i2 = 0; i2 < size; i2++) {
            char c;
            i iVar = (i) arrayList.get(i2);
            if (n) {
                c = iVar.j;
            } else {
                c = iVar.h;
            }
            char[] cArr = keyData.meta;
            if ((c == cArr[0] && (metaState & 2) == 0) || ((c == cArr[2] && (metaState & 2) != 0) || (n && c == 8 && i == 67))) {
                return iVar;
            }
        }
        return null;
    }

    public MenuItem getItem(int i) {
        return (MenuItem) this.f.get(i);
    }

    public void h(List<i> list, int i, KeyEvent keyEvent) {
        boolean n = n();
        int modifiers = keyEvent.getModifiers();
        KeyData keyData = new KeyData();
        if (keyEvent.getKeyData(keyData) || i == 67) {
            int size = this.f.size();
            for (int i2 = 0; i2 < size; i2++) {
                char c;
                int i3;
                i iVar = (i) this.f.get(i2);
                if (iVar.hasSubMenu()) {
                    iVar.o.h(list, i, keyEvent);
                }
                if (n) {
                    c = iVar.j;
                } else {
                    c = iVar.h;
                }
                if (n) {
                    i3 = iVar.k;
                } else {
                    i3 = iVar.i;
                }
                if (!(((modifiers & 69647) == (i3 & 69647) ? 1 : null) == null || c == 0)) {
                    char[] cArr = keyData.meta;
                    if ((c == cArr[0] || c == cArr[2] || (n && c == 8 && i == 67)) && iVar.isEnabled()) {
                        list.add(iVar);
                    }
                }
            }
        }
    }

    public boolean hasVisibleItems() {
        if (this.x) {
            return true;
        }
        int size = size();
        for (int i = 0; i < size; i++) {
            if (((i) this.f.get(i)).isVisible()) {
                return true;
            }
        }
        return false;
    }

    public void i() {
        ArrayList l = l();
        if (this.k) {
            Iterator it = this.u.iterator();
            int i = 0;
            while (it.hasNext()) {
                WeakReference weakReference = (WeakReference) it.next();
                m mVar = (m) weakReference.get();
                if (mVar == null) {
                    this.u.remove(weakReference);
                } else {
                    i |= mVar.h();
                }
            }
            if (i != 0) {
                this.i.clear();
                this.j.clear();
                int size = l.size();
                for (i = 0; i < size; i++) {
                    i iVar = (i) l.get(i);
                    if (iVar.g()) {
                        this.i.add(iVar);
                    } else {
                        this.j.add(iVar);
                    }
                }
            } else {
                this.i.clear();
                this.j.clear();
                this.j.addAll(l());
            }
            this.k = false;
        }
    }

    public boolean isShortcutKey(int i, KeyEvent keyEvent) {
        return g(i, keyEvent) != null;
    }

    public String j() {
        return "android:menu:actionviewstates";
    }

    public g k() {
        return this;
    }

    public ArrayList<i> l() {
        if (!this.h) {
            return this.g;
        }
        this.g.clear();
        int size = this.f.size();
        for (int i = 0; i < size; i++) {
            i iVar = (i) this.f.get(i);
            if (iVar.isVisible()) {
                this.g.add(iVar);
            }
        }
        this.h = false;
        this.k = true;
        return this.g;
    }

    public boolean m() {
        return this.w;
    }

    public boolean n() {
        return this.c;
    }

    public boolean o() {
        return this.d;
    }

    public void p() {
        this.k = true;
        q(true);
    }

    public boolean performIdentifierAction(int i, int i2) {
        return r(findItem(i), i2);
    }

    public boolean performShortcut(int i, KeyEvent keyEvent, int i2) {
        i g = g(i, keyEvent);
        boolean s = g != null ? s(g, null, i2) : false;
        if ((i2 & 2) != 0) {
            c(true);
        }
        return s;
    }

    public void q(boolean z) {
        if (this.p) {
            this.q = true;
            if (z) {
                this.r = true;
                return;
            }
            return;
        }
        if (z) {
            this.h = true;
            this.k = true;
        }
        if (!this.u.isEmpty()) {
            z();
            Iterator it = this.u.iterator();
            while (it.hasNext()) {
                WeakReference weakReference = (WeakReference) it.next();
                m mVar = (m) weakReference.get();
                if (mVar == null) {
                    this.u.remove(weakReference);
                } else {
                    mVar.f(z);
                }
            }
            y();
        }
    }

    public boolean r(MenuItem menuItem, int i) {
        return s(menuItem, null, i);
    }

    public void removeGroup(int i) {
        int size = size();
        int i2 = 0;
        while (i2 < size) {
            if (((i) this.f.get(i2)).b == i) {
                break;
            }
            i2++;
        }
        i2 = -1;
        if (i2 >= 0) {
            size = this.f.size() - i2;
            int i3 = 0;
            while (true) {
                int i4 = i3 + 1;
                if (i3 >= size || ((i) this.f.get(i2)).b != i) {
                    q(true);
                } else {
                    t(i2, false);
                    i3 = i4;
                }
            }
            q(true);
        }
    }

    public void removeItem(int i) {
        int size = size();
        int i2 = 0;
        while (i2 < size) {
            if (((i) this.f.get(i2)).a == i) {
                break;
            }
            i2++;
        }
        i2 = -1;
        t(i2, true);
    }

    /* JADX WARNING: Removed duplicated region for block: B:33:0x0062  */
    /* JADX WARNING: Removed duplicated region for block: B:30:0x0056  */
    public boolean s(android.view.MenuItem r7, b.b.g.i.m r8, int r9) {
        /*
        r6 = this;
        r7 = (b.b.g.i.i) r7;
        r0 = 0;
        if (r7 == 0) goto L_0x00d2;
    L_0x0005:
        r1 = r7.isEnabled();
        if (r1 != 0) goto L_0x000d;
    L_0x000b:
        goto L_0x00d2;
    L_0x000d:
        r1 = r7.p;
        r2 = 1;
        if (r1 == 0) goto L_0x0019;
    L_0x0012:
        r1 = r1.onMenuItemClick(r7);
        if (r1 == 0) goto L_0x0019;
    L_0x0018:
        goto L_0x0040;
    L_0x0019:
        r1 = r7.n;
        r1 = r1.e(r1, r7);
        if (r1 == 0) goto L_0x0022;
    L_0x0021:
        goto L_0x0040;
    L_0x0022:
        r1 = r7.g;
        if (r1 == 0) goto L_0x0036;
    L_0x0026:
        r3 = r7.n;	 Catch:{ ActivityNotFoundException -> 0x002e }
        r3 = r3.a;	 Catch:{ ActivityNotFoundException -> 0x002e }
        r3.startActivity(r1);	 Catch:{ ActivityNotFoundException -> 0x002e }
        goto L_0x0040;
    L_0x002e:
        r1 = move-exception;
        r3 = "MenuItemImpl";
        r4 = "Can't find activity to handle intent; ignoring";
        android.util.Log.e(r3, r4, r1);
    L_0x0036:
        r1 = r7.A;
        if (r1 == 0) goto L_0x0042;
    L_0x003a:
        r1 = r1.e();
        if (r1 == 0) goto L_0x0042;
    L_0x0040:
        r1 = 1;
        goto L_0x0043;
    L_0x0042:
        r1 = 0;
    L_0x0043:
        r3 = r7.A;
        if (r3 == 0) goto L_0x004f;
    L_0x0047:
        r4 = r3.a();
        if (r4 == 0) goto L_0x004f;
    L_0x004d:
        r4 = 1;
        goto L_0x0050;
    L_0x004f:
        r4 = 0;
    L_0x0050:
        r5 = r7.f();
        if (r5 == 0) goto L_0x0062;
    L_0x0056:
        r7 = r7.expandActionView();
        r1 = r1 | r7;
        if (r1 == 0) goto L_0x00d1;
    L_0x005d:
        r6.c(r2);
        goto L_0x00d1;
    L_0x0062:
        r5 = r7.hasSubMenu();
        if (r5 != 0) goto L_0x0073;
    L_0x0068:
        if (r4 == 0) goto L_0x006b;
    L_0x006a:
        goto L_0x0073;
    L_0x006b:
        r7 = r9 & 1;
        if (r7 != 0) goto L_0x00d1;
    L_0x006f:
        r6.c(r2);
        goto L_0x00d1;
    L_0x0073:
        r9 = r9 & 4;
        if (r9 != 0) goto L_0x007a;
    L_0x0077:
        r6.c(r0);
    L_0x007a:
        r9 = r7.hasSubMenu();
        if (r9 != 0) goto L_0x008e;
    L_0x0080:
        r9 = new b.b.g.i.r;
        r5 = r6.a;
        r9.m7839init(r5, r6, r7);
        r7.o = r9;
        r5 = r7.e;
        r9.setHeaderTitle(r5);
    L_0x008e:
        r7 = r7.o;
        if (r4 == 0) goto L_0x0095;
    L_0x0092:
        r3.f(r7);
    L_0x0095:
        r9 = r6.u;
        r9 = r9.isEmpty();
        if (r9 == 0) goto L_0x009e;
    L_0x009d:
        goto L_0x00cb;
    L_0x009e:
        if (r8 == 0) goto L_0x00a4;
    L_0x00a0:
        r0 = r8.e(r7);
    L_0x00a4:
        r8 = r6.u;
        r8 = r8.iterator();
    L_0x00aa:
        r9 = r8.hasNext();
        if (r9 == 0) goto L_0x00cb;
    L_0x00b0:
        r9 = r8.next();
        r9 = (java.lang.ref.WeakReference) r9;
        r3 = r9.get();
        r3 = (b.b.g.i.m) r3;
        if (r3 != 0) goto L_0x00c4;
    L_0x00be:
        r3 = r6.u;
        r3.remove(r9);
        goto L_0x00aa;
    L_0x00c4:
        if (r0 != 0) goto L_0x00aa;
    L_0x00c6:
        r0 = r3.e(r7);
        goto L_0x00aa;
    L_0x00cb:
        r1 = r1 | r0;
        if (r1 != 0) goto L_0x00d1;
    L_0x00ce:
        r6.c(r2);
    L_0x00d1:
        return r1;
    L_0x00d2:
        return r0;
        */
        throw new UnsupportedOperationException("Method not decompiled: b.b.g.i.g.s(android.view.MenuItem, b.b.g.i.m, int):boolean");
    }

    public void setGroupCheckable(int i, boolean z, boolean z2) {
        int size = this.f.size();
        for (int i2 = 0; i2 < size; i2++) {
            i iVar = (i) this.f.get(i2);
            if (iVar.b == i) {
                iVar.x = (iVar.x & -5) | (z2 ? 4 : 0);
                iVar.setCheckable(z);
            }
        }
    }

    public void setGroupDividerEnabled(boolean z) {
        this.w = z;
    }

    public void setGroupEnabled(int i, boolean z) {
        int size = this.f.size();
        for (int i2 = 0; i2 < size; i2++) {
            i iVar = (i) this.f.get(i2);
            if (iVar.b == i) {
                iVar.setEnabled(z);
            }
        }
    }

    public void setGroupVisible(int i, boolean z) {
        int size = this.f.size();
        Object obj = null;
        for (int i2 = 0; i2 < size; i2++) {
            i iVar = (i) this.f.get(i2);
            if (iVar.b == i && iVar.l(z)) {
                obj = 1;
            }
        }
        if (obj != null) {
            q(true);
        }
    }

    public void setQwertyMode(boolean z) {
        this.c = z;
        q(false);
    }

    public int size() {
        return this.f.size();
    }

    public final void t(int i, boolean z) {
        if (i >= 0 && i < this.f.size()) {
            this.f.remove(i);
            if (z) {
                q(true);
            }
        }
    }

    public void u(m mVar) {
        Iterator it = this.u.iterator();
        while (it.hasNext()) {
            WeakReference weakReference = (WeakReference) it.next();
            m mVar2 = (m) weakReference.get();
            if (mVar2 == null || mVar2 == mVar) {
                this.u.remove(weakReference);
            }
        }
    }

    public void v(Bundle bundle) {
        if (bundle != null) {
            SparseArray sparseParcelableArray = bundle.getSparseParcelableArray(j());
            int size = size();
            for (int i = 0; i < size; i++) {
                MenuItem item = getItem(i);
                View actionView = item.getActionView();
                if (!(actionView == null || actionView.getId() == -1)) {
                    actionView.restoreHierarchyState(sparseParcelableArray);
                }
                if (item.hasSubMenu()) {
                    ((r) item.getSubMenu()).v(bundle);
                }
            }
            int i2 = bundle.getInt("android:menu:expandedactionview");
            if (i2 > 0) {
                MenuItem findItem = findItem(i2);
                if (findItem != null) {
                    findItem.expandActionView();
                }
            }
        }
    }

    public void w(Bundle bundle) {
        int size = size();
        SparseArray sparseArray = null;
        for (int i = 0; i < size; i++) {
            MenuItem item = getItem(i);
            View actionView = item.getActionView();
            if (!(actionView == null || actionView.getId() == -1)) {
                if (sparseArray == null) {
                    sparseArray = new SparseArray();
                }
                actionView.saveHierarchyState(sparseArray);
                if (item.isActionViewExpanded()) {
                    bundle.putInt("android:menu:expandedactionview", item.getItemId());
                }
            }
            if (item.hasSubMenu()) {
                ((r) item.getSubMenu()).w(bundle);
            }
        }
        if (sparseArray != null) {
            bundle.putSparseParcelableArray(j(), sparseArray);
        }
    }

    public final void x(int i, CharSequence charSequence, int i2, Drawable drawable, View view) {
        Resources resources = this.b;
        if (view != null) {
            this.o = view;
            this.m = null;
            this.n = null;
        } else {
            if (i > 0) {
                this.m = resources.getText(i);
            } else if (charSequence != null) {
                this.m = charSequence;
            }
            if (i2 > 0) {
                Context context = this.a;
                Object obj = b.i.c.a.a;
                this.n = context.getDrawable(i2);
            } else if (drawable != null) {
                this.n = drawable;
            }
            this.o = null;
        }
        q(false);
    }

    public void y() {
        this.p = false;
        if (this.q) {
            this.q = false;
            q(this.r);
        }
    }

    public void z() {
        if (!this.p) {
            this.p = true;
            this.q = false;
            this.r = false;
        }
    }

    public MenuItem add(int i) {
        return a(0, 0, 0, this.b.getString(i));
    }

    public SubMenu addSubMenu(int i) {
        return addSubMenu(0, 0, 0, this.b.getString(i));
    }

    public MenuItem add(int i, int i2, int i3, CharSequence charSequence) {
        return a(i, i2, i3, charSequence);
    }

    public SubMenu addSubMenu(int i, int i2, int i3, CharSequence charSequence) {
        i iVar = (i) a(i, i2, i3, charSequence);
        r rVar = new r(this.a, this, iVar);
        iVar.o = rVar;
        rVar.setHeaderTitle(iVar.e);
        return rVar;
    }

    public MenuItem add(int i, int i2, int i3, int i4) {
        return a(i, i2, i3, this.b.getString(i4));
    }

    public SubMenu addSubMenu(int i, int i2, int i3, int i4) {
        return addSubMenu(i, i2, i3, this.b.getString(i4));
    }
}
